package lbframework.bean.textAreaEx;

import java.awt.Font;
import java.awt.event.FocusListener;
import java.awt.event.KeyListener;
import javax.swing.UIManager;
import javax.swing.text.PlainDocument;
import lbframework.classe.Util;
import lbframework.classe.LBException;

public class LBTextAreaEx extends javax.swing.JPanel {

    public final static String MASK_TEXTO = "Texto";
    private boolean obrigatorio = false;
    private String mascara = "";

    public LBTextAreaEx() {
        initComponents();
        setRodape();

        mascara = MASK_TEXTO;

    }

    public String getText() {
        return txtArea.getText();
    }

    public void setText(String i_text) {
        txtArea.setText(i_text);
        setRodape();
    }

    public void setCaixaAlta(boolean i_caixaAlta) {
        txtArea.setCaixaAlta(i_caixaAlta);
    }

    public boolean isCaixaAlta() {
        return txtArea.isCaixaAlta();
    }

    public void clear() {
        txtArea.setText("");

        setRodape();
    }

    private void setRodape() {
        lblCaracter.setText(String.valueOf(txtArea.getText().length()) + " de " + txtArea.getColumns());
    }

    public void setCaretPosition(int position) {
        txtArea.setCaretPosition(position);
    }

    public void setEditable(boolean b) {
        txtArea.setEditable(b);
    }

    @Override
    public void requestFocus() {
        txtArea.requestFocus();
    }

    public void setSelectionStart(int selectionStart) {
        txtArea.setSelectionStart(selectionStart);
    }

    public void setSelectionEnd(int selectionEnd) {
        txtArea.setSelectionEnd(selectionEnd);
    }

    public int getCaretPosition() throws Exception {
        return txtArea.getCaretPosition();
    }

    @Override
    public synchronized void addFocusListener(FocusListener l) {
        txtArea.addFocusListener(l);
    }

    @Override
    public void setEnabled(boolean enabled) {
        txtArea.setEnabled(enabled);

        setObrigatorio(obrigatorio);
    }

    @Override
    public synchronized void addKeyListener(KeyListener l) {
        txtArea.addKeyListener(l);
    }

    public boolean isObrigatorio() {
        return obrigatorio;
    }

    public void setObrigatorio(boolean obrigatorio) {
        this.obrigatorio = obrigatorio;

        if (txtArea.isEnabled()) {
            if (obrigatorio) {
                txtArea.setBackground(Util.COR_OBRIGATORIO);
            } else {
                txtArea.setBackground(UIManager.getColor("TextArea.background"));
            }
        } else {
            txtArea.setBackground(UIManager.getColor("TextArea.disabledBackground"));
        }
    }

    @Override
    public void setFont(Font font) {
        if (txtArea != null) {
            txtArea.setFont(font);
        }
    }

    @Override
    public Font getFont() {
        if (txtArea != null) {
            return txtArea.getFont();
        } else {
            return null;
        }
    }

    public String getMascara() {
        return txtArea.getMascara();
    }

    public void setMascara(String i_mascara) throws Exception {
        mascara = i_mascara;

        if (i_mascara.equals(MASK_TEXTO)) {
            txtArea.setMascara(i_mascara);
        } else {
            txtArea.setMascara("");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        vRPanel1 = new lbframework.bean.panel.LBPanel();
        vRLabel1 = new lbframework.bean.label.LBLabel();
        vRPanel2 = new lbframework.bean.panel.LBPanel();
        lblCaracter = new lbframework.bean.label.LBLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtArea = new lbframework.bean.textArea.LBTextArea();

        vRPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        vRLabel1.setText("Total de Caracteres:");

        javax.swing.GroupLayout vRPanel1Layout = new javax.swing.GroupLayout(vRPanel1);
        vRPanel1.setLayout(vRPanel1Layout);
        vRPanel1Layout.setHorizontalGroup(
            vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(vRLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        vRPanel1Layout.setVerticalGroup(
            vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(vRLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        vRPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout vRPanel2Layout = new javax.swing.GroupLayout(vRPanel2);
        vRPanel2.setLayout(vRPanel2Layout);
        vRPanel2Layout.setHorizontalGroup(
            vRPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblCaracter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(257, Short.MAX_VALUE))
        );
        vRPanel2Layout.setVerticalGroup(
            vRPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblCaracter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        txtArea.setColumns(20);
        txtArea.setRows(5);
        txtArea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtAreaKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtAreaKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtAreaKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(txtArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(vRPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(vRPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(vRPanel2, 0, 18, Short.MAX_VALUE)
                    .addComponent(vRPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtAreaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtAreaKeyReleased
        setRodape();
    }//GEN-LAST:event_txtAreaKeyReleased

    private void txtAreaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtAreaKeyTyped
    }//GEN-LAST:event_txtAreaKeyTyped

    private void txtAreaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtAreaKeyPressed
        setRodape();
    }//GEN-LAST:event_txtAreaKeyPressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private lbframework.bean.label.LBLabel lblCaracter;
    private lbframework.bean.textArea.LBTextArea txtArea;
    private lbframework.bean.label.LBLabel vRLabel1;
    private lbframework.bean.panel.LBPanel vRPanel1;
    private lbframework.bean.panel.LBPanel vRPanel2;
    // End of variables declaration//GEN-END:variables

    public int getColumns() {
        return txtArea.getColumns();
    }

    public void setColumns(int columns) {
        txtArea.setColumns(columns);

        setRodape();
    }

    public void setDocument(PlainDocument plainDocument) {
        txtArea.setDocument(plainDocument);
    }
}
